package com.huilian.tool.work.weixin.service.impl;

import com.huilian.tool.common.utils.RestTemplateUtil;
import com.huilian.tool.work.weixin.dto.Order;
import com.huilian.tool.work.weixin.service.AuthorizeService;
import com.huilian.tool.work.weixin.service.LicenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 少凡
 * @Date 2022-06-25 16:17
 * version 1.0
 */
@Slf4j
@Service
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private AuthorizeService authorizeService;

    @Resource
    private RestTemplateUtil restTemplateUtil;

    @Override
    public String createNewOrder(String corpId, String redisKey) {

        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/create_new_order?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        order.setBuyer_userid("ZhangFanYan");
        order.getAccount_count().setExternal_contact_count(1);
        order.getAccount_duration().setMonths(1);
       return restTemplateUtil.unifyPost(url, order, String.class);

    }

    @Override
    public String createRenewOrderJob(String corpId, String userId, Integer type, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/create_renew_order_job?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        order.getAccount_list().setUserid(userId);
        order.getAccount_list().setType(type);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String listOrder(String corpId, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/list_order?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String getOrder(String orderId, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/get_order?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setOrder_id(orderId);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String listOrderAccount(String orderId, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/list_order_account?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setOrder_id(orderId);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String activeAccount(String corpId, String userId, String activeCode, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/active_account?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
       order.setCorpid(corpId);
       order.setUserid(userId);
       order.setActive_code(activeCode);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String getActiveInfoByCode(String corpId, String activeCode, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/get_active_info_by_code?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        order.setActive_code(activeCode);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String listActiveAccount(String corpId, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/list_actived_account?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String getActiveInfoByUser(String corpId, String userId, String redisKey) {
        String url = new StringBuilder("https://qyapi.weixin.qq.com/cgi-bin/license/get_active_info_by_user?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        order.setUserid(userId);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }

    @Override
    public String batchTransferLicense(String corpId , List<Order.TransferList> transferList, String redisKey) {
        String url = new StringBuilder(" https://qyapi.weixin.qq.com/cgi-bin/license/batch_transfer_license?provider_access_token=")
                .append(authorizeService.getProviderAccessToken(redisKey)).toString();
        Order order = new Order();
        order.setCorpid(corpId);
        order.setTransfer_list(transferList);
        return restTemplateUtil.unifyPost(url, order, String.class);
    }




}
